package com.ljy.my_study.lintcode.两数之和;

/**
 * @author James
 * @date 2018年4月19日
 */
public class AddTwoNumbersMain {
	public static void main(String[] args) {

	}

	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		if (l1 == null && l2 == null) {
			return null;
		}
		int val = 0;
		if (l1 == null) {
			val += 0;
			l1 = new ListNode(0);
		} else {
			val += l1.val;
		}
		if (l2 == null) {
			val += 0;
			l2 = new ListNode(0);
		} else {
			val += l2.val;
		}
		int addNextNum = 0;
		if (val > 9) {
			addNextNum = val / 10;
			val = val - 10*addNextNum;
		}
		ListNode l3 = new ListNode(val);
		l3.next = addTwoNumbers(l1.next, l2.next);
		if (addNextNum > 0) {
			if (l3.next == null) {
				l3.next = new ListNode(0);
			}
//			l3.next.val += addNextNum;
			l3.next=addTwoNumbers(l3.next,new ListNode(addNextNum));
		}

		return l3;

	}

	public class ListNode {
		int val;
		ListNode next;

		ListNode(int x) {
			val = x;
		}
	}

}
